<?php
namespace app\index\controller;
use app\common\exception\System as SystemException;
use Exception;
class News extends Common
{
        /**
         * 不需要登录
         * @var array
         */
        protected $noNeedLogin = ['*'];
        /**
         * 新闻首页
         * @return [type] [description]
         */
        public function index()
        {
          $param = $this->request->param();
          $type = trim($param['type']??'imgtext');
          $page = trim($param['page']??1);
          $page_size = trim($param['page_size']??10);
          $res = service('index/News')->getMainNewsIndex($type,$param,$page,$page_size);
          return $this->successReturn($res,SystemException::E_SUCCESS, '获取成功');
        }
        /**
         * 新闻详情
         * @param strings $type 新闻类型
         * @param int $id 新闻ID
         * @return array 新闻内容数组
         */
        public function details(){
      	  $member_id = $this->auth->member_id;
          $param = $this->request->param();
          $type = trim($param['type']??'imgtext');
          $res = service('index/News')->getNewsInfo($type,$param['id']??0,$member_id);
          return $this->successReturn($res,SystemException::E_SUCCESS, '获取成功');
        }

        /**
         * 新闻提交
         * @param [type] $[name] [<description>]
         */
        public function save(){
              $param = $this->request->param(true);
              $type = trim($param['type']??'imgtext');
              $res = service('index/News')->saveNewsInfo($type,$param??[]);
              return $this->successReturn($res,SystemException::E_SUCCESS,'发表成功');
        }

        /**
         * 支付
         * @param stings $type 支付种类
         */
        public function pay()
        {
              $param = $this->request->param(true);
              $type = trim($param['mtype']??'imgtext');
              $res = service('index/News')->payNewsPrice($type,$param??[]);
              return $this->successReturn($res,SystemException::E_SUCCESS,'支付成功');
        }

        /**
         * 评论点赞
         */
        public function givegood(){
              $param = $this->request->request();
              $member_id = $param['member_id']??0;
              $id = $param['comment_id']??0;
              $res = service('index/News')->savechangeGood($id,$member_id);
              preg_match_all('/[\x{4e00}-\x{9fff}]+/u', $res['msg'],$cn_name);
              $res['msg'] = implode('',$cn_name[0])??$res['msg'];
              return $this->successReturn($res,SystemException::E_SUCCESS,$res['msg']);
        }
        /*
      	新闻点赞田鑫
        **/
      	public function newsGivegood(){
      		$param = $this->request->request();
      		$member_id = $param['member_id']??0;
             $news_id = $param['news_id']??0;
             $res=service('index/News')->newsGivegood($news_id,$member_id);
             return $this->successReturn($res,SystemException::E_SUCCESS,$res['msg']);
       }
        /**
         * 发表评论
         */
        public function csave(){
              $param = $this->request->request();
              $type = trim($param['type']??0);
              $res = service('index/News')->saveCommentInfo($type,$param??[]);
              return $this->successReturn($res,SystemException::E_SUCCESS,'发表成功');
        }

        /**
         * 关注文章
         *
         */
        public function addfollow(){
        	$param = $this->request->request();
        	$type = trim($param['type']??'imgtext');
        	$res = service('index/News')->saveFollowInfo($type,$param??[]);
        	return $this->successReturn($res,SystemException::E_SUCCESS,$res['msg']);
        }
        /**
         * 关注列表
         *
         */
        public function followlist(){
        	$param = $this->request->request();
        	$type = trim($param['type']??0);
        	$res = service('index/News')->followList($type,$param??[],true);
        	return $this->successReturn($res,SystemException::E_SUCCESS,$res['msg']);
        }
      	/*
      	关注田鑫
      	*/
      	public function newsFollow(){
      		$param = $this->request->request();
      		$data['member_id']=$param['member_id'];
      		$data['follow_member_id']=$param['follow_member_id'];
      		$res=service('index/News')->newsFollow($data);
      		return $this->successReturn($res,SystemException::E_SUCCESS,$res['msg']);
      	}
      	public function addNews(){
      		$data=$this->request->param();
             $data['member_id']=$this->auth->member_id;
             $model = new \app\index\model\Imgtext;
             $res=$model->allowField(true)->save($data);
             if($res){
                 $res=['status'=>1,'msg'=>'提交成功'];
                 return $this->successReturn($data,SystemException::E_SUCCESS, $res['msg']); 
           }else{
                 $res=['status'=>0,'msg'=>'提交失败'];
                 return $this->successReturn($res,SystemException::E_SUCCESS, $res['msg']);      
           }
     }
}